package com.kaisa.activity.ui;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.kaisa.activity.BaseApp;
import com.kaisa.activity.R;
import com.kaisa.activity.bean.HomeEntity;
import com.kaisa.activity.bean.TopEntity;
import com.kaisa.activity.constant.UrlHttp;
import com.mob.tools.utils.LocalDB;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by 123 on 2015/12/14.
 */
@ContentView(R.layout.hometop_main)
public class HomeTopActivity extends AppCompatActivity implements View.OnClickListener {
    @ViewInject(R.id.top_web)
    private WebView webView;
    @ViewInject(R.id.top_back)
    private ImageView backImageView;
    @ViewInject(R.id.top_title)
    private TextView titleTextView;
    @ViewInject(R.id.top_share)
    private ImageView shareImageView;
    private TopEntity entity;
    private HomeEntity entitys;
    private int result;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        entitys = (HomeEntity) bundle.getSerializable("ben");
        result = intent.getIntExtra("position", 0);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new webViewClient());
        webView.loadUrl(entitys.getTopPage().getActivityInfo().get(result).getActUrl());
        titleTextView.setText(entitys.getTopPage().getActivityInfo().get(result).getHeading());
        initData();
    }

    private void initData() {
        backImageView.setOnClickListener(this);
        shareImageView.setOnClickListener(this);
        x.http().get(new RequestParams(UrlHttp.PATH_HOME_TOP), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                entity = JSON.parseObject(s, TopEntity.class);
            }

            @Override
            public void onError(Throwable throwable, boolean b) {

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_share:
                BaseApp.getInstance().showShare(entitys.getTopPage().getActivityInfo().get(result).getHeading()
                        , entitys.getTopPage().getActivityInfo().get(result).getActUrl());
                break;
            case R.id.top_back:
                this.finish();
                break;
        }
    }

    class webViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        //重写shouldOverrideUrlLoading方法，使点击链接后不使用其他的浏览器打开。
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d("hehe","---------------------------------------------"+url);
            view.addJavascriptInterface(new Caissa(), "caissa");
            Intent intent = new Intent(HomeTopActivity.this, ProduceActivity.class);
            BaseApp.getInstance().toastShow(view.getChildCount() + "");
            intent.putExtra("id", view.getVerticalScrollbarPosition());
            startActivity(intent);
            //如果不需要其他对点击链接事件的处理返回true，否则返回false
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            view.loadUrl("javascript:window.bridge.showSource('<body>'+"
                    + "document.body.innerHTML+'</body>');");
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
        }
    }

    public class Bridge {
        @android.webkit.JavascriptInterface
        public void showSource(String html) {
            Log.d("hehe", "-------showSource" + html);
            content = html;
        }
    }

    final class Caissa {
        @android.webkit.JavascriptInterface
        public void toTourDetail(String json) {
            Log.d("hehe", "--------------------------------toTourDetail:" + json);
        }

        @android.webkit.JavascriptInterface
        public void toHolidayDetail(String json) {
            Log.d("hehe", "-------------------------------toHolidayDetail:" + json);
        }

        @android.webkit.JavascriptInterface
        public void toCommonSearchResultSemiBuffet(String json) {
            Log.d("hehe", "------------------------------toCommonSearchResultSemiBuffet:" + json);
        }

        @android.webkit.JavascriptInterface
        public void getUser(String kson) {
            BaseApp.getInstance().toastShow(kson);
        }
    }
}
